package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.Certification;
import cn.stylefeng.guns.modular.business.mapper.CertificationMapper;
import cn.stylefeng.guns.modular.business.model.dto.CertificationDto;
import cn.stylefeng.guns.modular.business.model.params.CertificationParam;
import cn.stylefeng.guns.modular.business.model.result.CertificationResult;
import cn.stylefeng.guns.modular.business.service.CertificationService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 实名认证 服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-07-25
 */
@Service
public class CertificationServiceImpl extends ServiceImpl<CertificationMapper, Certification> implements CertificationService {

    @Autowired
    private UserService userService;

    @Override
    public void add(CertificationParam param) {
        Certification entity = getEntity(param);
        this.save(entity);
    }

    /**
     * 实名认证
     *
     * @param certificationDto
     * @author pzp
     * @Date 2019-07-25
     */
    @Override
    public void addOrUpdate(CertificationDto certificationDto) {
        //校验用户是否做过实名认证
        Certification certification = this.getOne(new QueryWrapper<Certification>().eq("user_id", certificationDto.getUserId()));
        if (null == certification) {
            certification = new Certification();
            ToolUtil.copyProperties(certificationDto, certification);
            certification.setCreateTime(new Date());
            certification.setUpdateTime(new Date());
            certification.setStatus("0");
            this.save(certification);
        } else {
            ToolUtil.copyProperties(certificationDto, certification);
            certification.setUpdateTime(new Date());
            certification.setStatus("0");
            this.updateById(certification);
        }
    }

    /**
     * 实名认证明细
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Certification detail(long userId) {
        return this.getOne(new QueryWrapper<Certification>().eq("delete_flag", RecoverConst.DEL_FLAG_FALSE).eq("user_id", userId));
    }

    /**
     * 查看实名认证详情
     *
     * @param certificationId 实名认证id
     * @return
     */
    @Override
    public CertificationResult findById(Integer certificationId) {
        Certification certification = Optional.ofNullable(this.getById(certificationId)).orElseThrow(()-> new ServiceException(400,"查询实名认证失败"));
        CertificationResult certificationResult=new CertificationResult();
        ToolUtil.copyProperties(certification,certificationResult);
        certificationResult.setUserName(Optional.ofNullable(userService.getById(certification.getUserId())).orElse(new User()).getName());
        return certificationResult;
    }

    @Override
    public void delete(CertificationParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CertificationParam param) {
        Certification oldEntity = getOldEntity(param);
        Certification newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CertificationResult findBySpec(CertificationParam param) {
        return null;
    }

    @Override
    public List<CertificationResult> findListBySpec(CertificationParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(String status, String userName) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, status, userName);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CertificationParam param) {
        return param.getCertificationId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Certification getOldEntity(CertificationParam param) {
        return this.getById(getKey(param));
    }

    private Certification getEntity(CertificationParam param) {
        Certification entity = new Certification();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 通过用户id获取实名认证信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Certification findByUserId(long userId) {
        return this.getOne(new QueryWrapper<Certification>().eq("delete_flag", RecoverConst.DEL_FLAG_FALSE).eq("user_id", userId));
    }
}
